package com.ywb;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

/**
 * @Author ywb
 */

public class ESIndexClient {
    public static void main(String[] args) throws Exception{

        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );
        // Create
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("java-demo");
        CreateIndexResponse createIndexResponse =
                esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse.isAcknowledged());

        // Retrieve
        GetIndexRequest getIndexRequest = new GetIndexRequest("java-demo");
        GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getAliases());

        // // Delete
        // DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("java-demo");
        // AcknowledgedResponse deleteResponse = esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        // System.out.println(deleteResponse.isAcknowledged());

        esClient.close();

    }
}
